package com.jidu.service.impl;

import java.rmi.ServerException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jidu.common.exception.ServiceException;
import com.jidu.common.utils.DateUtils;
import com.jidu.common.utils.StringUtils;
import com.jidu.domain.CourseStudentRel;
import com.jidu.domain.SysClass;
import com.jidu.domain.SysTeacher;
import com.jidu.mapper.SysClassMapper;
import com.jidu.mapper.SysTeacherMapper;
import com.jidu.service.ICourseStudentRelService;
import com.jidu.service.ISysClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


/**
 * 班级Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-19
 */
@Service
public class SysClassServiceImpl  extends ServiceImpl<SysClassMapper, SysClass> implements ISysClassService
{
    @Autowired
    private SysClassMapper sysClassMapper;
    @Autowired
    private ICourseStudentRelService courseStudentRelService;
    @Autowired
    private SysTeacherMapper teacherMapper;

    /**
     * 查询班级
     * 
     * @param id 班级主键
     * @return 班级
     */
    @Override
    public SysClass selectSysClassById(Long id)
    {
        return sysClassMapper.selectSysClassById(id);
    }

    /**
     * 查询班级列表
     * 
     * @param sysClass 班级
     * @return 班级
     */
    @Override
    public List<SysClass> selectSysClassList(SysClass sysClass)
    {
        return sysClassMapper.selectSysClassList(sysClass);
    }

    /**
     * 新增班级
     * 
     * @param sysClass 班级
     * @return 结果
     */
    @Override
    public int insertSysClass(SysClass sysClass)
    {
        sysClass.setCreateTime(DateUtils.getNowDate());
        sysClass.setUpdateTime(new Date());
        sysClass.setOrgId(1L);
        sysClass.setDelFlag("0");
        sysClass.setState("0");
        return sysClassMapper.insertSysClass(sysClass);
    }

    /**
     * 修改班级
     * 
     * @param sysClass 班级
     * @return 结果
     */
    @Override
    public int updateSysClass(SysClass sysClass)
    {
        sysClass.setUpdateTime(DateUtils.getNowDate());
        return sysClassMapper.updateSysClass(sysClass);
    }

    /**
     * 批量删除班级
     * 
     * @param ids 需要删除的班级主键
     * @return 结果
     */
    @Override
    public int deleteSysClassByIds(Long[] ids)
    {
        return sysClassMapper.deleteSysClassByIds(ids);
    }

    @Override
    public List<Map<String, Object>> classTwoList(Long classId) {
        return sysClassMapper.classTwoList(classId);
    }

    @Override
    public int updateSysClassByIds(Long[] ids) {
        boolean b = false;
        if (ids != null && ids.length>0){
            for (Long id : ids) {
                Long count = courseStudentRelService.lambdaQuery().eq(CourseStudentRel::getDelFlag, "0")
                        .in(CourseStudentRel::getStatus, new String[]{"0", "1"})
                        .in(CourseStudentRel::getIsChangeClass, new String[]{"0", "2"})
                        .eq(CourseStudentRel::getClassId, id).count();
                if (count > 0){
                    throw new ServiceException("班级还有学生存在,无法删除！");
                }
            }
            b = this.lambdaUpdate().set(SysClass::getDelFlag, "1").in(SysClass::getId, ids).update();
            b = true;
        }
        if (b){
            return 1;
        }else {
            return -1;
        }
    }

    @Override
    public List<Map<String, Object>> classList(String className, Long teacherId) {
        return sysClassMapper.classList(className,teacherId);
//        List<Map<String, Object>> resultList = new ArrayList<>();
//        List<CourseStudentRel> list = courseStudentRelService.lambdaQuery().eq(CourseStudentRel::getDelFlag, "0")
//                .in(CourseStudentRel::getIsChangeClass, new String[]{"0", "2"})
//                .in(CourseStudentRel::getStatus,new String[]{"0","1"})
//                .list();
//        if (CollectionUtils.isEmpty(list)){
//            return new ArrayList<>();
//        }
//        Map<Long, List<CourseStudentRel>> collect = list.stream().collect(Collectors.groupingBy(CourseStudentRel::getClassId));
//        for (Map.Entry<Long, List<CourseStudentRel>> longListEntry : collect.entrySet()) {
//            Map<String, Object> hashMap = new HashMap<>();
//            SysClass one = this.lambdaQuery().eq(SysClass::getId, longListEntry.getKey()).eq(SysClass::getDelFlag, "0").one();
//            if (one != null){
//                hashMap.put("className",one.getName());
//                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                hashMap.put("createTime",simpleDateFormat.format(one.getCreateTime()));
//            }
//            hashMap.put("stuNums",longListEntry.getValue().size());
//            SysTeacher sysTeacher = teacherMapper.selectSysTeacherById(longListEntry.getValue().get(0).getTeacherId());
//            if (sysTeacher != null){
//                hashMap.put("teacherName",sysTeacher.getName());
//                hashMap.put("teacherId",sysTeacher.getId());
//            }
//            resultList.add(hashMap);
//        }
//        if (!StringUtils.isEmpty(className)){
//            resultList = resultList.stream().filter(m->className.contains((String)m.get("className"))).collect(Collectors.toList());
//        }
//        if (!StringUtils.isEmpty(className)){
//            resultList = resultList.stream().filter(m->teacherId.equals((Long) m.get("teacherId"))).collect(Collectors.toList());
//        }
//        return resultList;
    }

    /**
     * 删除班级信息
     * 
     * @param id 班级主键
     * @return 结果
     */
    @Override
    public int deleteSysClassById(Long id)
    {
        return sysClassMapper.deleteSysClassById(id);
    }
}
